package com.holyn.guofang.model.sharepreference;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Holyn on 2017/6/5.
 */
//分配倾向
public class AppSharePreference {

    private static SharedPreferences sharedPreferences = null;

    public static final String L_ENGLISH = "en";//en_US
    public static final String L_CHINESE = "zh";//zh_CN

    public static void newInstance(Context context){
        if (sharedPreferences == null){
            sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
    }

    public static void setAppLanguage(Context context, String type){
        newInstance(context);
        if (type.equals(L_CHINESE)  || type.equals(L_ENGLISH)){
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("language", type);
            editor.commit();
        }
    }

    public static String getAppLanguage(Context context){
        newInstance(context);
        return sharedPreferences.getString("language", L_CHINESE);
    }

    public static void saveIsRememberPassword(Context context, boolean isRemember){
        editorCommitBoolean(context, "remember_password", isRemember);
    }

    public static boolean getIsRememberPassword(Context context){
        return getBoolean(context, "remember_password", false);
    }

    public static void saveIsFirstOpen(Context context, boolean isFirstOpen){
        editorCommitBoolean(context, "is_first_open", isFirstOpen);
    }

    public static boolean getIsFirstOpen(Context context){
        return getBoolean(context, "is_first_open", true);
    }

    private static void editorCommit(Context context, String key, String value){
        newInstance(context);
        if (value == null) value = "";
        sharedPreferences.edit().putString(key, value).commit();
    }

    private static void editorCommitBoolean(Context context, String key, boolean value){
        newInstance(context);
        sharedPreferences.edit().putBoolean(key, value).commit();
    }

    private static String getString(Context context, String key, String defValue){
        newInstance(context);
        return sharedPreferences.getString(key, defValue);
    }

    private static boolean getBoolean(Context context, String key,boolean defValue){
        newInstance(context);
        return sharedPreferences.getBoolean(key, defValue);
    }

}
